import { defineStore } from 'pinia'
import { setStorage, getStorage } from '@/utils/storage'
import {
  UserLogin, 
  GetUserInfo, 
  RefreshToken, 
} from '@/api/user'
import appSetting from '@/configs/appSetting'
const { ruizhiTokenKey } = appSetting

export default defineStore('userinfo', {
  state: () => {
    const localToken = getStorage(ruizhiTokenKey)
    return {
      token: localToken,
      avatarUrl: '',
      nickname: '',
      isVip: false,
      activation: '未登录',
      hasLogin: false,
      userType: null,
      probationExpired: null,
      accessAll: false,
      navigationMenus: [
        {id: 1, name: '资讯报告', code: 1000, path: '/inforeport', key: 'SEC001', access: false, hasChildren: false, 'subShow': false},
        {id: 2, name: '策略分析', code: 2000, path: '/strategy', key: 'SEC002', access: false, hasChildren: false, 'subShow': false},
        {id: 3, name: '行业数据', code: 3000, path: '/datalib', key: 'SEC004', access: false, hasChildren: false, 'subShow': false},
        {id: 4, name: '定制数据', code: 4000, path: '/flexDatalib', key: 'SEC005', access: false, hasChildren: false, 'subShow': false},
        {id: 5, name: '计算平台', code: 5000, path: '/calculate', key: 'SEC006', access: false, hasChildren: false, 'subShow': false},
        {id: 6, name: '交易诊断', code: 6000, path: '/exdoctor', key: 'SEC007',access: false,  hasChildren: false, 'subShow': false},
        // {id: 8, name: '期货工具', code: 8000, path: '/ftools', key: 'SEC008',access: true,  hasChildren: false, 'subShow': false},
      ],
      _loginLoading: false,
    }
  },
  getters: {},
  actions: {
    appUserLogin(data) {
      return new Promise((resolve, reject) => {
        this._loginLoading = true
        UserLogin(data).then((res) => {
          // console.log('store里登录', res)
          if (res.success) {
            const useToken = res.data.token
            setStorage(useToken, ruizhiTokenKey)
            this.token = useToken
          }
          this._loginLoading = false
          resolve(res)
        }).catch((err) => {
          this._loginLoading = false
          reject(err)
        })
      })
    },

    refreshToken() {
      return new Promise((resolve, reject) => {
        RefreshToken().then((res) => {
          const useToken = res.data.token
          setStorage(useToken, ruizhiTokenKey)
          this.token = useToken
          resolve(res)
        }).catch((err) => {
          this.appUserLogout()
          reject(err)
        })
      })
    },

    getAppUserInfo() {
      return new Promise((resolve, reject) => {
        GetUserInfo().then((res) => {
          // 设置资料后，返回数据
          const data = res.data
          this.avatarUrl = data.avatar
          this.nickname = data.nickname
          this.isVip = data.is_vip
          this.activation = data.activation
          this.signinDays = data.signin_days
          this.hasLogin = true
          this.userType = data.user_type
          this.probationExpired = data.probation_expired
          // 设置功能权限
          this.accessAll = data.access_all
          for (let i=0; i < this.navigationMenus.length; i++) {
            for (let j=0; j < data.access_sections.length; j++) {
              if (this.navigationMenus[i].key === data.access_sections[j].sec_key) {
                this.navigationMenus[i].access = true
              }
            }
          }
          // setStorage(1, ruizhiLoginKey)
          // console.log('Store里用户信息返回', data)
          // 获取菜单
          // UserNavMenu().then((res) => {
          //   this.navigationMenus = res.data.navigation
          //   // console.log(this.navigationMenus)
          // })
          resolve(res)
        }).catch((err) => {
          this.appUserLogout()
          reject(err)
        })
      })
    },

    // getNavigationMenu() {
    //   UserNavMenu().then((res) => {
    //     this.navigationMenus = res.data.navigation
    //   })
    // },

    appUserLogout() {
      this.nickname = ''
      this.activation = '未登录'
      this.signinDays = 0
      this.hasLogin = false
      // this.navigationMenus = []
      // setStorage(0, ruizhiLoginKey)
      setStorage('', ruizhiTokenKey)
      this.token = ''
    },
  },
})